package com.web.new12306.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.omg.IOP.TAG_MULTIPLE_COMPONENTS;
import com.web.new12306.dao.TicketDao;
import com.web.new12306.pojo.Ticket;

public class TicketDaoImpl implements TicketDao {
	/**
	 * @author 赵靖懿
	 */
	
	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;

	@Override
	public List<Ticket> select(Connection conn, Integer page, Integer rows) {
		// TODO Auto-generated method stub
		String sql = "select * from ticket limit ?,?";
		List<Ticket> list = new ArrayList<Ticket>();
		try {
			this.conn=conn;
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, (page-1)*rows);
			pstmt.setInt(2, page*rows);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				Ticket ticket = new Ticket();
				ticket.setTicketId(rs.getInt("ticketId"));
				ticket.setTrainNumber(rs.getString("trainNumber"));
				ticket.setStartStation(rs.getString("startStation"));
				ticket.setTerminalStation(rs.getString("terminalStation"));
				ticket.setTime(rs.getTimestamp("time"));
				ticket.setPrice(rs.getInt("price"));
				ticket.setRemainingTickets(rs.getInt("remainingTickets"));
				list.add(ticket);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

	@Override
	public boolean updateTicket(Connection conn, Ticket t) {
		// TODO Auto-generated method stub
		boolean flag=false;
		String sql="update ticket set price=?,trainNumber=?,time=?,startStation=?,terminalStation=?,remainingTickets=? where ticketId=?";
		try {
			this.conn=conn;
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, t.getPrice());
			pstmt.setString(2, t.getTrainNumber());
			pstmt.setTimestamp(3, new java.sql.Timestamp(t.getTime().getTime()));
			pstmt.setString(4, t.getStartStation());
			pstmt.setString(5, t.getTerminalStation());
			pstmt.setInt(6, t.getRemainingTickets());
			pstmt.setInt(7,t.getTicketId());
			if(pstmt.executeUpdate()>0)
			{
				flag=true;
			}else {
				flag=false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public int number(Connection conn) {
		// TODO Auto-generated method stub
		String sql="select count(*) from ticket";
		int n=0;
		try {
			this.conn=conn;
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				n=rs.getInt(1);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return n;
	}

	/**
	 * @author 封梦琳
	 */
	public void insertTicket(Connection conn, Ticket t) {
		String sql="insert into ticket (trainNumber,startStation,terminalStation,time,price,remainingTickets)values(?,?,?,?,?,?)";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, t.getTrainNumber());
			pstmt.setString(2, t.getStartStation());
			pstmt.setString(3, t.getTerminalStation());
		    pstmt.setTimestamp(4, new java.sql.Timestamp(t.getTime().getTime()));
			pstmt.setInt(5, t.getPrice());
		    pstmt.setInt(6, t.getRemainingTickets());
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @author 封梦琳
	 */
	public boolean deleteTicket(Connection conn, Ticket t) {
		// TODO Auto-generated method stub
		String sql="delete  from ticket where ticketId=?";
		boolean flag=false;
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, t.getTicketId());
			if(pstmt.executeUpdate()>0)
			{
				flag=true;
			}else {
				flag=false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}

}
